package bxpay

import (
	"encoding/json"
	"errors"
	"fmt"
	"strconv"
	"time"

	"gitee.com/liuxuezhan/ar-platform/pay-channel-packet/bxpay/bxpay/tool"
)

type WithdrawParam struct {
	MerId    string `json:"merId"`
	OrderId  string `json:"orderId"`
	NonceStr string `json:"nonceStr"`
	Sign     string `json:"sign"`
}

type WithdrawResp struct {
	Code int           `json:"code"`
	Msg  string        `json:"msg"`
	Time string        `json:"time"`
	Data WithdrawParam `json:"data"`
}
type PayResp struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Time string `json:"Time"`
	Data Param  `json:"data"`
}

type Param struct {
	PayUrl     string `json:"payurl"`     // 支付页面地址，跳转即可
	OrderNo    string `json:"orderno"`    // 商户单号
	SysOrderNo string `json:"sysorderno"` // 平台系统单号
	Sign       string `json:"sign"`       // 签名
}

// 代收（充值） bPay
func BxPay(tempParams map[string]any) (resp PayResp, err error) {
	// 请求第三方接口
	params := make(map[string]any)
	params["merId"] = tempParams["merId"]
	params["orderId"] = tempParams["orderID"]
	params["orderAmt"] = tempParams["amount"]
	params["channel"] = tempParams["channel"]
	params["desc"] = tempParams["desc"]
	params["userId"] = tempParams["userId"]
	params["ip"] = tempParams["IP"]
	params["notifyUrl"] = tempParams["NotifyUrl"]
	params["returnUrl"] = tempParams["ReturnUrl"]
	params["nonceStr"] = strconv.FormatInt(time.Now().Unix(), 10)
	if v, err := tool.Sign(params, tempParams["SecretKey"].(string), tempParams["PrivateKey"].(string)); err != nil {
		return resp, errors.New("ErrSignGenerateFailed")
	} else {
		params["sign"] = v
	}
	output, err := tool.SendPostFormUrlEncoded(tempParams["url"].(string), params)

	if err != nil {
		return resp, err
	}
	var result PayResp
	err = json.Unmarshal([]byte(output), &result)
	if err != nil {
		return resp, err
	}
	if result.Code != 1 {
		return resp, errors.New(result.Msg)
	}
	return result, nil
}

// 代付（提现）Withdraw
func Withdraw(tempParam map[string]any) (resp WithdrawResp, err error) {
	// 请求第三方接口
	params := make(map[string]any)
	params["merId"] = tempParam["mid"]
	params["orderId"] = tempParam["orderId"]
	params["money"] = tempParam["amount"]
	params["acc_name"] = tempParam["accName"]
	params["acc_no"] = tempParam["accNo"]
	params["acc_code"] = tempParam["accCode"]
	if tempParam["province"] != "" {
		params["province"] = tempParam["province"]
	}
	if tempParam["city"] != "" {
		params["city"] = tempParam["city"]
	}
	if tempParam["zhihang"] != "" {
		params["zhihang"] = tempParam["zhihang"]
	}
	params["nonceStr"] = strconv.FormatInt(time.Now().Unix(), 10)
	params["otherpara1"] = tempParam["otherpara1"]
	params["otherpara2"] = tempParam["otherpara2"]
	params["notifyUrl"] = tempParam["NotifyUrl"]
	if v, err := tool.Sign(params, tempParam["SecretKey"].(string), tempParam["PrivateKey"].(string)); err != nil {
		return resp, errors.New("ErrSignGenerateFailed")
	} else {
		params["sign"] = v
	}
	output, err := tool.SendPostFormUrlEncoded(tempParam["url"].(string), params)

	if err != nil {
		return resp, fmt.Errorf("sendErr:%v", err)
	}

	var result WithdrawResp
	err = json.Unmarshal([]byte(output), &result)
	if err != nil {
		return result, fmt.Errorf("json.Unmarshal111111:%v", err, output)
	}
	if result.Code != 1 {
		return result, fmt.Errorf("code != 1:%v", err)
	}
	return result, nil
}
